package GUI.Zonas;

import BE.BELogAudit;
import BE.BEUsuario;
import BL.BLCombo;
import BL.BLLogAudit;
import BL.BLZona;
import com.sun.org.apache.bcel.internal.generic.AALOAD;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Timestamp;
import java.util.Date;
import javax.swing.JOptionPane;
    
public class GUIAgregarZona extends javax.swing.JDialog {
    BEUsuario objUsuario;
    
    /** Creates new form GUIAgregarZona */
    public GUIAgregarZona(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.inicializa();
    }

    GUIAgregarZona(java.awt.Frame object, boolean b, BEUsuario Usuario) {
        super(object, b);
        
        objUsuario=Usuario;
        
        initComponents();
        this.inicializa();
    }

    public void inicializa()
    {
        this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
        
        BLCombo bLCombo = new BLCombo();
        bLCombo.setCombo("TIPOPRODUCTO",1,cmbTipo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAgregar = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        pnlDatosZona1 = new javax.swing.JPanel();
        lblNombre1 = new javax.swing.JLabel();
        txtNombre1 = new javax.swing.JTextField();
        lblDescripcion1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtarDescripcion1 = new javax.swing.JTextArea();
        lblTipo = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Zona");

        btnAgregar.setText("Aceptar");
        btnAgregar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAgregar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnClose.setText("Cancelar");
        btnClose.setMaximumSize(new java.awt.Dimension(100, 25));
        btnClose.setPreferredSize(new java.awt.Dimension(100, 25));
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        pnlDatosZona1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Zona"));

        lblNombre1.setText("Nombre:");

        lblDescripcion1.setText("Descripción:");

        txtarDescripcion1.setColumns(20);
        txtarDescripcion1.setRows(5);
        jScrollPane2.setViewportView(txtarDescripcion1);

        lblTipo.setText("Tipo:");

        javax.swing.GroupLayout pnlDatosZona1Layout = new javax.swing.GroupLayout(pnlDatosZona1);
        pnlDatosZona1.setLayout(pnlDatosZona1Layout);
        pnlDatosZona1Layout.setHorizontalGroup(
            pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosZona1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcion1)
                    .addComponent(lblTipo)
                    .addComponent(lblNombre1))
                .addGap(18, 18, 18)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNombre1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                    .addComponent(cmbTipo, 0, 156, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnlDatosZona1Layout.setVerticalGroup(
            pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDatosZona1Layout.createSequentialGroup()
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre1)
                    .addComponent(txtNombre1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTipo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlDatosZona1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcion1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDatosZona1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(82, Short.MAX_VALUE)
                .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlDatosZona1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        BLZona blZona = new BLZona();
        int retorno;

        BLCombo idTipo = (BLCombo)cmbTipo.getSelectedItem();
        retorno = blZona.agregaZona(txtNombre1.getText(),idTipo.id,
                                    txtarDescripcion1.getText());
        if (retorno == 0)
        {
            BLLogAudit objgestorAdit = new BLLogAudit();
            BELogAudit objlogAudit= new BELogAudit();
            
            objlogAudit.setIdUsuario(objUsuario.getIdUsuario());
            objlogAudit.setIdInterface(0);
            objlogAudit.setIdUsuario(objUsuario.getIdUsuario());
            objlogAudit.setAccion("Registro de Zona por el usuario "+objUsuario.getUsuario()+"." );
            Date aux= new Date();
            Timestamp fecha= new Timestamp(aux.getTime());
            
            objlogAudit.setFecha(fecha);
            
            objgestorAdit.registrarAccion(objlogAudit);
            
            
            JOptionPane.showMessageDialog(null,"Se ingreso la zona correctamente",
                                              "Información",JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GUIAgregarZona dialog = new GUIAgregarZona(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnClose;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblDescripcion1;
    private javax.swing.JLabel lblNombre1;
    private javax.swing.JLabel lblTipo;
    private javax.swing.JPanel pnlDatosZona1;
    private javax.swing.JTextField txtNombre1;
    private javax.swing.JTextArea txtarDescripcion1;
    // End of variables declaration//GEN-END:variables

}
